package com.frame.pageform.component.tree;

import java.util.*;

import com.frame.pageform.component.BaseJsonObject;

public class TreeJosonObject extends BaseJsonObject{
	private List nodeList;

	public TreeJosonObject(){
		nodeList=new ArrayList();
	}

	public void add(TreeNode node){
		nodeList.add(node);
	}
	public String toString(){
		if(nodeList==null)
			return "";
		StringBuffer sb = new StringBuffer();
		try{
			sb.append("[");
			for(int i=0;i<nodeList.size();i++){
				if(i>0)
					sb.append(",");
				sb.append(toString((TreeNode)nodeList.get(i)));
			}
			sb.append("]");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return sb.toString();
	}
	public String toString(TreeNode node) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("\"key\":"+quote(node.getId()));
		sb.append(",\"title\":"+quote(node.getText()));
		if(node.isLazy()){
			//动态加载树
			if(node.getChildSize()==null)
				throw new Exception("TreeNode的childSize属性没有赋值");
			if(node.getChildSize().intValue()>0){
				sb.append(",\"isFolder\":"+true);
				sb.append(",\"isLazy\":"+true);
			}
		}else{
			if(node.getChildList()!=null && node.getChildList().size()>0)
				sb.append(",\"isFolder\":"+true);

		}
		if(node.isChecked())
			sb.append(",\"select\":"+true);
		if(node.isExpand())
			sb.append(",\"expand\":"+true);
		if(node.getUrl()!=null)
			sb.append(",\"url\":"+quote(node.getUrl()));
		if(node.getIcon()!=null && !"".equals(node.getIcon()))
			sb.append(",\"icon\":"+quote(node.getIcon()));
		if(node.getAuth()!=null && !"".equals(node.getAuth()))
			sb.append(",\"auth\":"+quote(node.getAuth()));
		if(node.getAttribute1()!=null && !"".equals(node.getAttribute1()))
			sb.append(",\"attribute1\":"+quote(node.getAttribute1()));
		if(node.getAttribute2()!=null && !"".equals(node.getAttribute2()))
			sb.append(",\"attribute2\":"+quote(node.getAttribute2()));
		if(node.isHideCheckbox())
			sb.append(",\"hideCheckbox\":true");
		List clist=node.getChildList();
		if(clist!=null){
			String cstr="";
			for(int i=0;i<clist.size();i++){
				TreeNode cnode=(TreeNode)clist.get(i);
				if(i>0)
					cstr+=",";
				cstr+=toString(cnode);

			}
			sb.append(",\"children\":["+cstr+"]");
		}
		return "{"+sb.toString()+"}";
	}

}
